<template>
  <div class="app-container">    <!-- 高级查询 -->

    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="商城:">
        <el-select v-model="queryParams.mallName" placeholder="请选择商城编号" clearable size="mini">
          <el-option v-for="option in options.mallOptions" :key="option.customerName" :label="option.customerName" :value="option.customerName" />
        </el-select>
      </el-form-item>
      <el-form-item label="SPU:">
        <el-input v-model="queryParams.custSpuId" size="small" placeholder="请输入SPU" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="汇总自:">
         <el-select v-model="queryParams.jobMonthFr" filterable clearable size="mini">
           <el-option v-for="item in options.jobMonthList" :key="item.key" :label="item.label" :value="item.key" />
         </el-select>
       </el-form-item>
       <el-form-item label="至:">
         <el-select v-model="queryParams.jobMonthTo" filterable clearable size="mini">
           <el-option v-for="item in options.jobMonthList" :key="item.key" :label="item.label" :value="item.key" />
         </el-select>
       </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" height="550" highlight-current-row @selection-change="handleSelectionChange" @sort-change="handleSortChange">
    <!--   <el-table-column type="selection" width="55" align="center" />
   -->
      <el-table-column label="序号" type="index" />

      <el-table-column label="SPU名称" align="center" width="532" prop="cargoObjectName" sortable />
       <el-table-column label="SPU" align="center" width="132" prop="custSpuId" sortable />
       <el-table-column label="供货数量" align="center"  width="132" prop="sumQty" />

<el-table-column label="销售金额" align="center" width="132"  prop="sumAmtAr">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.sumAmtAr).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <el-table-column label="供货金额" align="center" width="132"  prop="sumAmount">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.sumAmount).toFixed(2) }}</span>
         </template>
       </el-table-column>


    </el-table>

    <!-- 分页 -->
    <!-- 导入对话框 -->
    <el-dialog :title="uploadDialog.title" :visible.sync="uploadDialog.visible" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        :headers="uploadData.headers"
        :action="uploadData.url + '?updateSupport=' + uploadData.updateSupport"
        :data="uploadData.params"
        :disabled="uploadData.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        accept=".xlsx, .xls"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div slot="tip" class="el-upload__tip">
          <el-select v-model="uploadData.params.customerNo" style="width: 100%" filterable clearable placeholder="请选择下单客户" @change="handleSelected">
            <el-option v-for="option in options.customers" :key="option.id" :label="option.customerName" :value="option.id" />
          </el-select>

          <el-link type="info" style="font-size:12px" @click="handleDownloadTemplate">下载模板</el-link>
        </div>
        <div slot="tip" class="el-upload__tip" style="color:red">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmUploadDialog">确 定</el-button>
        <el-button @click="resetUploadDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import SalesOrderApi from '@/api/order/SalesOrderApi'
import AccountingApApi from '@/api/accounting/AccountingApApi'
import AccApSkuMonthlySettlementApi from '@/api/accounting/AccApSkuMonthlySettlementApi'
import SalesOrderItemApi from '@/api/order/SalesOrderItemApi'
import CustomerApi from '@/api/basic/CustomerApi'
import DictValueApi from '@/api/system/DictValueApi'
import appConstant from '@/constant/AppConstant'
import AuthUtils from '@/utils/AuthUtils'
export default {
  name: 'OmsSalesOrderList',
  filters: {
    settleStatusFilter(item) {
      const values = appConstant.settleStatusList.filter(value => value.key === item)
      if (values && values.length > 0) {
        return values[0].label
      } else {
        return ''
      }
    }
   },
  data() {
    return {
      options: {
        sysYnTypeOptions: [],
        orderTypeOptions: [],
        sourceTypeOptions: [],
        mallOptions: [],
        settleStatusOptions: appConstant.settleStatusList,
        salesOrderStatusOptions: [],
        customers: [],
       jobMonthList: this.$constant.jobMonthOptions,
        payTypeOptions: []
      },
      queryParams: {
        brandName: null,
        mallName: null,
        jobMonthFr: null,
        jobMonthTo: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      uploadDialog: {
        title: '订单批量导入',
        visible: false
      },
      uploadData: {
        headers: { Authorization: 'Bearer ' + AuthUtils.getToken() }, // 上传请求头
        url: process.env.VUE_APP_BASE_API + '/order/salesOrderItem/importData', // 上传的地址
        params: {
          customerNo: null
        },
        isUploading: false, // 是否禁用上传
        updateSupport: 0 // 是否更新已经存在的数据
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
     this.initParam()
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    initParam() {
       this.queryParams.jobMonthFr = this.parseTime(this.getBeforeDay(183), '{y}-{m}')
       this.queryParams.jobMonthTo = this.parseTime(new Date(), '{y}-{m}')
       this.queryParams.mallName = "南航会员商城"
       console.log(this.queryParams)
     },
    loadOptions() {
     let  that = this
     CustomerApi.list().then(response => {
       that.options.mallOptions = response.data
     })
    },
    handleAdd() {
      this.$router.push('/salesOrder/form/')
    },
    handleApAmtBtnShow: function(status) {
      const validStatusList = ['depart', 'delivery', 'poded', 'submitted']
      const index = validStatusList.indexOf(status)
      if (index === -1) {
        return false
      } else {
        return true
      }
    },
    handleSortChange(sort) {
      if (sort.order) {
        this.queryParams.sortProperty = sort.prop
        this.queryParams.sortDirection = sort.order === 'ascending' ? 'asc' : 'desc'
      } else {
        this.queryParams.sortProperty = null
        this.queryParams.sortDirection = null
      }
      this.handleQuery()
    },
    handleRemove(row) {
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SalesOrderApi.remove(row.id)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    generateSettlement() {
       AccountingApApi.generateArAp().then(() => {
         this.$message.success('生成结算成功!')
         this.handleQuery()
       })
    },
    handleEdit(row) {
      this.$router.push({ path: '/salesOrder/form/' + row.id })
    },
    handleQuery() {
      this.loading = true
      AccApSkuMonthlySettlementApi.spuRateView(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出所有销售订单数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SalesOrderItemApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    openUploadDialog() {
      this.resetUploadDialog()
      this.uploadDialog.visible = true
    },
    confirmUploadDialog() {
      this.$refs.upload.submit()
    },
    resetUploadDialog() {
      this.uploadDialog.visible = false
      this.uploadData.params.customerNo = null
    },
    handleDownloadTemplate() {
      SalesOrderApi.importTemplate().then(response => {
        this.$common.download(response.data)
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.uploadData.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.uploadDialog.visible = false
      this.uploadData.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.data ? response.data : response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.queryParams.status = null
      this.handleQuery()
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    },
    orderTypeFormat(type) {
      return this.$common.getDictValueName(this.options.orderTypeOptions, type)
    },
    settleStatusFormat(type) {
      return this.$common.getDictValueName(this.options.settleStatusOptions, type)
    },
    orderSourceFormat(source) {
      return this.$common.getDictValueName(this.options.sourceTypeOptions, source)
    },
    orderStatusFormat(status) {
      return this.$common.getDictValueName(this.options.salesOrderStatusOptions, status)
    },
    payTypeFormat(type) {
      return this.$common.getDictValueName(this.options.payTypeOptions, type)
    }
  }
}
</script>
